package com.huawei.esdk.tp.device.smc.v100r003c10.convert;

import java.util.ArrayList;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.domain.model.bean.VideoCapbilityItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.AddSite;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfFilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfSortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfVdieoCapbilityItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfVideoFormat;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.DateTimeFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.DoubleFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.EditSite;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.FilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.FilterColumn2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IPAddressEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IPAddressFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IntFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.PageParam;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.PagesInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryConfig;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QuerySitesResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SiteParamEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SiteType;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SortType;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.StringFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VideoFormat;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VideoProtocol;

public class TPSMC20V1R3C10SiteMgrConvert
{
    
    public AddSite addSiteInfoModel2Soap(Site sitesInfo)
    {
        ArrayOfVdieoCapbilityItem videoCap = new ArrayOfVdieoCapbilityItem();
        List<VideoCapbilityItem> videoCapbility = sitesInfo.getVideoCapbilitys();
        
        if (null != videoCapbility && !videoCapbility.isEmpty())
        {
            com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem vdieoCapbilityItem = null;
            
            for (VideoCapbilityItem vdieoCapbilityItemModel : videoCapbility)
            {
                vdieoCapbilityItem = new com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem();
                vdieoCapbilityItem.setVideo(getVideoString2Enum(vdieoCapbilityItemModel.getVideoProtocol()));
                
                ArrayOfVideoFormat formats = new ArrayOfVideoFormat();
                List<Integer> videoFormatModel = vdieoCapbilityItemModel.getVideoFormats();
                
                if (null != videoFormatModel && !videoFormatModel.isEmpty())
                {
                    for (Integer format : videoFormatModel)
                    {
                        formats.getVideoFormat().add(getFormatString2Enum(format));
                    }
                }
                
                vdieoCapbilityItem.setFormats(formats);
                
                videoCap.getVdieoCapbilityItem().add(vdieoCapbilityItem);
            }
        }
        
        SiteParamEx site = new SiteParamEx();
        site.setName(sitesInfo.getName());
        site.setRate(sitesInfo.getRate());
        site.setRegPassword(sitesInfo.getRegPassword());
        site.setRegUser(sitesInfo.getRegUser());
        
        // DTS2015012607447 siteInfo内容为空，添加会场信息，返回2130000010错误
        // 在北向加上参数判断，如果必填参数为空，则返回错误码
        site.setType(getTypeString2Enum(sitesInfo.getType()));
        
        site.setUri(sitesInfo.getUri());
        site.setVideoCap(videoCap);
        
        AddSite resMsg = new AddSite();
        resMsg.setOrgId(sitesInfo.getOrgId());
        resMsg.setSite(site);
        
        return resMsg;
    }
    
    private SiteType getTypeString2Enum(Integer value)
    {
        switch (value)
        {
            case 0:
                return SiteType.NONE;
            case 1:
                return SiteType.SITE_TYPE_AUTO;
            case 2:
                return SiteType.SITE_TYPE_E_1;
            case 3:
                return SiteType.SITE_TYPE_ISDN;
            case 4:
                return SiteType.SITE_TYPE_IP;
            case 5:
                return SiteType.SITE_TYPE_PSTN;
            case 6:
                return SiteType.SITE_TYPE_4_E_1;
            case 7:
                return SiteType.SITE_TYPE_SIP;
            case 8:
                return SiteType.SITE_TYPE_VO_IP_SIP;
            case 9:
                return SiteType.SITE_TYPE_VO_IP_H_323;
            case 10:
                return SiteType.SITE_TYPE_SIP_TP;
            case 11:
                return SiteType.SITE_TYPE_H_323_TP;
            default:
                return null;
        }
    }
    
    private VideoFormat getFormatString2Enum(Integer value)
    {
        switch (value)
        {
            case 0:
                return VideoFormat.AUTO;
            case 1:
                return VideoFormat.CIF_4;
            case 2:
                return VideoFormat.P_720;
            case 3:
                return VideoFormat.P_1080;
            case 4:
                return VideoFormat.P_480;
            case 5:
                return VideoFormat.P_720_60_F;
            case 6:
                return VideoFormat.P_1080_60_F;
            case 7:
                return VideoFormat.Q_CIF;
            case 8:
                return VideoFormat.CIF;
            case 9:
                return VideoFormat.SQ_CIF;
            case 10:
                return VideoFormat.CIF_2;
            case 11:
                return VideoFormat.I_108;
            case 12:
                return VideoFormat.I_480;
            default:
                return null;
        }
    }
    
    private static VideoProtocol getVideoString2Enum(Integer value)
    {
        switch (value)
        {
            case 0:
                return VideoProtocol.AUTO;
            case 1:
                return VideoProtocol.H_261;
            case 2:
                return VideoProtocol.H_263;
            case 3:
                return VideoProtocol.H_264;
            case 4:
                return VideoProtocol.H_264_HP;
            default:
                return null;
        }
    }
    
    public EditSite editSiteInfoModel2Soap(Site sitesInfo)
    {
        ArrayOfVdieoCapbilityItem videoCap = new ArrayOfVdieoCapbilityItem();
        List<VideoCapbilityItem> videoCapbility = sitesInfo.getVideoCapbilitys();
        
        if (null != videoCapbility && !videoCapbility.isEmpty())
        {
            com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem vdieoCapbilityItem = null;
            
            for (VideoCapbilityItem vdieoCapbilityItemModel : videoCapbility)
            {
                vdieoCapbilityItem = new com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem();
                vdieoCapbilityItem.setVideo(getVideoString2Enum(vdieoCapbilityItemModel.getVideoProtocol()));
                
                ArrayOfVideoFormat formats = new ArrayOfVideoFormat();
                List<Integer> videoFormatModel = vdieoCapbilityItemModel.getVideoFormats();
                
                if (null != videoFormatModel && !videoFormatModel.isEmpty())
                {
                    for (Integer format : videoFormatModel)
                    {
                        formats.getVideoFormat().add(getFormatString2Enum(format));
                    }
                }
                
                vdieoCapbilityItem.setFormats(formats);
                
                videoCap.getVdieoCapbilityItem().add(vdieoCapbilityItem);
            }
        }
        
        SiteParamEx site = new SiteParamEx();
        site.setName(sitesInfo.getName());
        site.setRate(sitesInfo.getRate());
        site.setRegPassword(sitesInfo.getRegPassword());
        site.setRegUser(sitesInfo.getRegUser());
        site.setType(getTypeString2Enum(sitesInfo.getType()));
        site.setUri(sitesInfo.getUri());
        site.setVideoCap(videoCap);
        
        EditSite resMsg = new EditSite();
        resMsg.setSite(site);
        
        return resMsg;
    }
    
    public com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QuerySites querySitesModal2Soap(QuerySites query)
    {
        com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QuerySites south =
            new com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QuerySites();
        QueryConfig config = new QueryConfig();
        // 排序参数
        ArrayOfSortItem sortItem = new ArrayOfSortItem();
        if (null != query.getSortField())
        {
            //做排序用
            for (SortField item : query.getSortField())
            {
                SortItem sortItemSoap = new SortItem();
                sortItemSoap.setSort(0 == item.getIsAscend() ? SortType.ASC : SortType.DESC);
                sortItemSoap.setItemIndex(filterColumn2Int2Enum(item.getFieldIndex()));
                sortItem.getSortItem().add(sortItemSoap);
            }
            
        }
        config.setSortItems(sortItem);
        // 过滤参数
        ArrayOfFilterBase filter = new ArrayOfFilterBase();
        if (null != query.getQueryCondition())
        {
            //做过滤用
            for (QueryCondition item : query.getQueryCondition())
            {
                FilterBase fielter = getFilterModal2Soap(item);
                if (null != fielter)
                {
                    filter.getFilterBase().add(fielter);
                }
            }
            
        }
        config.setFilters(filter);
        // 焦点数据
        config.setFocusItem(query.getFocusItem());
        // 分页数据'
        PageParam page = new PageParam();
        page.setCurrentPage(query.getCurrentPage());
        // 设定每页数为会场总数，一次性查出
        page.setNumberPerPage(query.getNumberPerPage());
        config.setPageParam(page);
        
        south.setQueryConfig(config);
        return south;
    }
    
    public SDKResult<PagedList<Site>> querySitesSoap2Modal(QuerySitesResponse southResponse)
    {
        SDKResult<PagedList<Site>> result = new SDKResult<PagedList<Site>>();
        PagedList<Site> pagedList = new PagedList<Site>();
        if (null == southResponse)
        {
            result.setResult(pagedList);
            return result;
        }
        
        // 书写返回值
        result.setErrCode(southResponse.getQuerySitesResult().getErrCode());
        result.setSubSystem(String.valueOf(southResponse.getQuerySitesResult().getSubSystem()));
        result.setDescription(southResponse.getQuerySitesResult().getDescription());
        if (null != southResponse.getSites() && southResponse.getSites().getSiteParamEx().size() > 0)
        {
            List<SiteParamEx> siteParamEx = southResponse.getSites().getSiteParamEx();
            Site site = null;
            for (int i = 0; i < siteParamEx.size(); i++)
            {
                site = new Site();
                site.setUri(siteParamEx.get(i).getUri());
                site.setName(siteParamEx.get(i).getName());
                site.setSiteType(siteParamEx.get(i).getType().toString());
                site.setRate(siteParamEx.get(i).getRate());
                site.setRegUser(siteParamEx.get(i).getRegUser());
                site.setRegPassword(siteParamEx.get(i).getRegPassword());
                
                if (null != siteParamEx.get(i).getVideoCap())
                {
                    List<VideoCapbilityItem> videoCapbilitys =
                        getVideoCapbilitysSoap2Modal(siteParamEx.get(i).getVideoCap().getVdieoCapbilityItem());
                    site.setVideoCapbilitys(videoCapbilitys);
                }
                pagedList.addRecord(site);
            }
        }
        
        PagesInfo pageInfo = southResponse.getPageInfo();
        if (null != pageInfo)
        {
            pagedList.setCurPageNo(pageInfo.getCurrentPage());
            pagedList.setTotalPageCount(pageInfo.getTotalPages());
            pagedList.setTotalRecordCount(pageInfo.getTotalRecords());
        }
        
        result.setResult(pagedList);
        return result;
    }
    
  // findbugs 方法名首字母小写
    private FilterColumn2 filterColumn2Int2Enum(Integer val)
    {
        switch (val)
        {
            case 0:
                return FilterColumn2.SITE_NAME;
            case 1:
                return FilterColumn2.SITE_URI;
            case 2:
                return FilterColumn2.SITE_STATUS;
            case 3:
                return FilterColumn2.SITE_TYPE;
            case 4:
                return FilterColumn2.CONF_TEMPLATE_NAME;
            case 5:
                return FilterColumn2.CONF_TEMPLATE_ACCESS_CODE;
            case 6:
                return FilterColumn2.MCU_NAME;
            default:
                return null;
        }
        
    }
    
    private FilterBase getFilterModal2Soap(QueryCondition item)
    {
        
        if (null == item)
        {
            return null;
        }
        if ("1".equals(item.getConditionType()))
        {
            if (2 == item.getConditionValue().length)
            {
                StringFilter stringFilter = new StringFilter();
                stringFilter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                stringFilter.setValue((String)item.getConditionValue()[1]);
                return stringFilter;
            }
            else
            {
                return null;
            }
        }
        else if ("2".equals(item.getConditionType()))
        {
            if (4 == item.getConditionValue().length)
            {
                IntFilter filter = new IntFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Integer)item.getConditionValue()[1]);
                filter.setLessThanValue((Integer)item.getConditionValue()[2]);
                filter.setEqualToValue((Integer)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("3".equals(item.getConditionType()))
        {
            if (4 == item.getConditionValue().length)
            {
                DoubleFilter filter = new DoubleFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Double)item.getConditionValue()[1]);
                filter.setLessThanValue((Double)item.getConditionValue()[2]);
                filter.setEqualToValue((Double)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("4".equals(item.getConditionType()))
        {
            if (3 == item.getConditionValue().length)
            {
                DateTimeFilter filter = new DateTimeFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((XMLGregorianCalendar)item.getConditionValue()[1]);
                filter.setLessThanValue((XMLGregorianCalendar)item.getConditionValue()[2]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("5".equals(item.getConditionType()))
        {
            if (2 == item.getConditionValue().length)
            {
                IPAddressFilter filter = new IPAddressFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                IPAddressEx value = new IPAddressEx();
                value.setIPAddress((String)item.getConditionValue()[0]);
                filter.setValue(value);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
        
    }
    
    private List<VideoCapbilityItem> getVideoCapbilitysSoap2Modal(
        List<com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem> list)
    {
        List<VideoCapbilityItem> result = new ArrayList<VideoCapbilityItem>();
        if (null != list)
        {
            for (com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.VdieoCapbilityItem item : list)
            {
                VideoCapbilityItem vdieoCapbilityItem = new VideoCapbilityItem();
                if (null != item.getFormats() && null != item.getFormats().getVideoFormat())
                {
                    List<Integer> formatItems = new ArrayList<Integer>();
                    for (VideoFormat formatItem : item.getFormats().getVideoFormat())
                    {
                        formatItems.add(getVideoFormatEnum2Int(formatItem));
                    }
                    vdieoCapbilityItem.setVideoFormats(formatItems);
                }
                vdieoCapbilityItem.setVideoProtocol(getVideoProtocolEnum2Int(item.getVideo()));
                result.add(vdieoCapbilityItem);
            }
        }
        return result;
    }
    
    private Integer getVideoFormatEnum2Int(VideoFormat val)
    {
        final int AUTO = 0;
        final int CIF_4 = 1;
        final int P_720 = 2;
        final int P_1080 = 3;
        final int P_480 = 4;
        final int P_720_60_F = 5;
        final int P_1080_60_F = 6;
        final int Q_CIF = 7;
        final int CIF = 8;
        final int SQ_CIF = 9;
        final int CIF_2 = 10;
        final int I_108 = 11;
        final int I_480 = 12;
        
        if (null == val)
        {
            return null;
        }
        
        switch (val)
        {
            case AUTO:
            {
                return AUTO;
            }
            case CIF_4:
            {
                return CIF_4;
            }
            case P_720:
            {
                return P_720;
            }
            case P_1080:
            {
                return P_1080;
            }
            case P_480:
            {
                return P_480;
            }
            case P_720_60_F:
            {
                return P_720_60_F;
            }
            case P_1080_60_F:
            {
                return P_1080_60_F;
            }
            case Q_CIF:
            {
                return Q_CIF;
            }
            case CIF:
            {
                return CIF;
            }
            case SQ_CIF:
            {
                return SQ_CIF;
            }
            case CIF_2:
            {
                return CIF_2;
            }
            case I_108:
            {
                return I_108;
            }
            case I_480:
            {
                return I_480;
            }
            
            default:
            {
                return null;
            }
        }
    }
    
    private Integer getVideoProtocolEnum2Int(VideoProtocol val)
    {
        final int AUTO = 0;
        final int H_261 = 1;
        final int H_263 = 2;
        final int H_264 = 3;
        final int H_264_HP = 4;
        
        if (null == val)
        {
            return null;
        }
        
        switch (val)
        {
            case AUTO:
            {
                return AUTO;
            }
            case H_261:
            {
                return H_261;
            }
            case H_263:
            {
                return H_263;
            }
            case H_264:
            {
                return H_264;
            }
            case H_264_HP:
            {
                return H_264_HP;
            }
            default:
            {
                return null;
            }
        }
    }
}
